/**
 * 风险讲解
 * Created by KY on 2017/8/18.
 */
define(['ldfw/core', 'art', 'text!tpl/product/risk_explanation.tpl','comps/session',
        'comps/ajax', 'comps/audio','ldfw/utils/format', 'ldfw/helper/formatHelper'],
    function (LDFW, art, tpl, session, ajax, audio) {
        var View = LDFW.Container.extend({
            className: 'panel',
            tpl: tpl,
            events:{
                'change [type="radio"]':'changeHandler',
                'click [operate="next"]':'next'
            },
            initialize:function(){
                this.on('shown',this.loadData)
            },
            rendered:function() {
            },
            loadData:function(){
                const _this = this
                // 查询风险说明文件
                ajax.post('pfL.9.103',{
                    relation_field:'23',
                    pd_no:this.pd_no
                }).success(function(resp){
                    if(resp.result && resp.result.length) {
                        let file = resp.result[0];
                        ajax.preview('fileDownload', {
                            fileId: file.file_id
                        }).success(function (blob) {
                            let objectUrl = URL.createObjectURL(blob)
                            audio.init(_this.$el.find('.audio-container'), {
                                musicSrc: objectUrl,
                                from: current_company,
                                title: '风险讲解'
                            })
                        })
                    }
                })
                // ajax.preview('/assets/2.mp3',null,{notProcessUrl:true,type:'GET'}).success(function(blob){
                //     let objectUrl = URL.createObjectURL(blob)
                //         audio.init(_this.$el.find('.audio-container'), {
                //             musicSrc: objectUrl,
                //             from: current_company,
                //             title: '风险讲解'
                //         })
                // })
            },
            changeHandler:function(){
                let enable = true
                $('input[type="radio"]:checked').each(function(){
                  if($(this).val()!=1){
                      enable = false;
                      return
                  }
                })
                if(enable){
                  this.enableButton()
                }else{
                  this.disableButton()
                }
            },
            disableButton:function(){
                let $button = this.$el.find('[operate="next"]')
                $button.addClass('weui-btn_disabled')
                $button.attr('disabled','disabled')
            },
            enableButton:function(){
                let $button = this.$el.find('[operate="next"]')
                $button.removeClass('weui-btn_disabled')
                $button.removeAttr('disabled')
            },
            setProductNo:function(pd_no){
                this.pd_no = pd_no
            },
            next:function(){
                //留痕
                ajax.post('cpliL.3.102',{
                    mark_type:13,
                    relation_field:session.getItem('time_stamp'),
                    pd_no:this.pd_no
                })
                LDFW.navigate('product/risk_warn/'+this.pd_no)
            }
        })
        return new View()
    })